// pages/mall/search/search.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
let http = new Http()

const pageSize = 6
var pageNum = 1
var pages = 1
var isLoadMore = false

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchWord: '',
    hotSearch: [],
    searchHistory: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pageNum: 1,
      pageSize: 6,
      pages: 1,
      goods: []
    })
    this.getHistory()
    this.getMallHotSearch()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.searchWord.length > 0) {
      this.getGoodList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 自定义方法
   */
  selectToSearch: function (e) {
    var searchWord = e.currentTarget.dataset.searchWord
    this.setData({
      searchWord: searchWord
    })
    this.search()
  },

  search: function () {
    var searchWord = this.data.searchWord
    if (searchWord.length > 0) {
      this.setData({
        pageNum: 1,
        pages: 1,
        goods: []
      })
      this.addHistory(searchWord)
      this.getGoodList();
    } else {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
    }
  },

  getGoodList: function(){
    var that = this
    var pageNum = this.data.pageNum
    var pageSize = this.data.pageSize
    var pages = this.data.pages
    var searchWord = this.data.searchWord
    if (pageNum <= pages) {
      http.sendHttp(Api.search_goods, {
        data: {
          pageNum: pageNum,
          pageSize: pageSize,
          searchWord: searchWord
        },
        success: function (res) {
          var showEmpty = false
          var goods = that.data.goods.concat(res.data.obj.goods)
          if(goods.length == 0){
            showEmpty = true
          }
          that.setData({
            goods: goods,
            pages: res.data.obj.pages,
            pageNum: pageNum + 1,
            showEmpty: showEmpty
          })
        }
      })
    }
  },

  bindInput: function (e) {
    this.setData({
      searchWord: e.detail.value
    })
    if (e.detail.value.length <= 0) {
      this.setData({
        goods: []
      })
    }
  },

  delHistory: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否要删除所有历史记录？',
      success(res) {
        if (res.confirm) {
          wx.setStorageSync('searchHistory', [])
          that.getHistory()
        }
      }
    })
  },

  getHistory: function () {
    var that = this
    wx.getStorage({
      key: 'searchHistory',
      success: function(res) {
        that.setData({
          searchHistory: res.data
        })
      },
    })
  },

  addHistory: function (searchWord) {
    var searchHistory = this.data.searchHistory
    var index = searchHistory.indexOf(searchWord)
    if (index > -1) {
      searchHistory.splice(index, 1)
    }
    if (searchHistory.length >= 8) {
      searchHistory.pop()
    }
    searchHistory.unshift(searchWord)
    wx.setStorageSync('searchHistory', searchHistory);
    this.getHistory()
  },

  getMallHotSearch: function () {
    var that = this
    http.sendHttp(Api.get_mall_hot_search, {
      data: { },
      success: function (res) {
        that.setData({
          hotSearch: res.data.obj.hotSearchs
        })
      }
    })
  },

  tapAddToCart: function(event){
    var goodId = event.currentTarget.dataset.idx
    http.sendHttp(Api.add_goods_to_cart, {
      data: {
        goodsId: goodId
      },
      success: function (res) {
        wx.showToast({
          title: '加入成功'
        })
      }
    }, 'POST')
  },

  goGoodsDetail: function (e) {
    var goodsId = e.currentTarget.dataset.goodsId
    wx.navigateTo({
      url: '/pages/mall/goodsDetail/goodsDetail?goodsId=' + goodsId
    })
  }
})